package net.lab1024.sa.admin.module.business.order.Scheduled;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.order.dao.OrderDao;
import net.lab1024.sa.admin.module.business.order.domain.entity.OrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderStatusTask {

    @Autowired
    private OrderDao orderDao;

    // 每分钟检查一次（可根据需求调整cron表达式）
    @Scheduled(cron = "0 * * * * ?")
    public void updateExpiredOrderStatus() {
        log.info("开始检查过期订单状态...");

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 查询所有状态为2或3且dead_time小于当前时间的订单
        List<OrderEntity> expiredOrders = orderDao.selectExpiredOrders(now);
//        List<OrderEntity> uselessOrders = orderDao.selectUselessOrders();
        if (!expiredOrders.isEmpty()) {
            // 批量更新状态为7
            expiredOrders.forEach(order -> {
                order.setOrderState(7L); // 7表示已过期或其他目标状态
                orderDao.updateById(order);
            });

            log.info("成功更新{}个过期订单状态", expiredOrders.size());
        }
//        if (!uselessOrders.isEmpty()) {
//
//            uselessOrders.forEach(order -> {
////                order.setOrderState(7L); // 7表示已过期或其他目标状态
//                orderDao.deleteByOrderId(order.getOrderId());
//            });
//
//            log.info("删除{}个未保存订单", uselessOrders.size());
//        }
    }
}